import database as db;
from readLinks import getRev
from readLinks import getRev1
'''
	This program calculates the value of each revision corresponding to different metrics.
	The output is stored in 'pageValues.txt'
'''



import config as cg
from math import sqrt

wordList = [];
freqList = [];
ageList = [];
prevFreqList = [];
averageAgeList = [];
averageFreqList = [];

def calculateValueOfRev():
	global freqList;
	global ageList;
	global averageFreqList
	global averageAgeList

	pageValue = 0;	
	pageValuef = 0;	
	pageValuefRoot = 0;
	pageValueARoot = 0;
	pageValueAvgf = 0;
	pageValueAvgA = 0;
	i = 0;
	while True:
		try:
			if freqList[i] > 0:
				pageValue += ageList[i];
				pageValuef += freqList[i]*ageList[i];
				pageValuefRoot += sqrt(abs(freqList[i]))*ageList[i];
				sqrtAge = sqrt(ageList[i]);
				pageValueARoot += freqList[i]*sqrtAge;
				#print "yes" + str(i)
				pageValueAvgf += averageFreqList[i]*ageList[i];
				#print "no"
				pageValueAvgA += averageAgeList[i]*ageList[i];

			i += 1
			
		except:
			#print "Value Calculated, now returning"
			#print "xsgsdg " + str(pageValueARoot) + "  " + str(i);
			#return pageValue, pageValuef, pageValuefRoot, pageValueARoot;
			return pageValue, pageValuef, pageValuefRoot, pageValueARoot, pageValueAvgf, pageValueAvgA;
		
def initWordList():
	global freqList
	i = 0
	while True:
		try:
			freqList[i] = 0;
			i += 1
		except:
			#print 'List Complete'
			return;
	


def cleanWordList():
	
	global freqList
	global ageList
	global prevFreqList

	i = 0
	while True:
		try:
			prevFreqList[i] = freqList[i];
			#print "No problemo"
			if freqList[i] == 0:
				freqList[i] = -1
				ageList[i] = 0;
			i += 1
		except:
			#print "List Cleaning Complete"
			return;				
			
def addWord( word ):
	
	global wordList;
	global freqList;
	global ageList;
	global prevFreqList;
	global averageFreqList;
	global averageAgeList;

	if word is not None:
		wordList.append(word)
		freqList.append(1)
		ageList.append(1)
		prevFreqList.append(0);
		averageFreqList.append(0);
		averageAgeList.append(0);
		#print "Word Added " + word
	
def updateWordList( word ):
	global wordList;

	if wordList == []:
		addWord(word)
		
	else:
		try:
			i = wordList.index(word);
			if( freqList[i] == 0 ):
				freqList[i] = 1;
				ageList[i] += 1;
				
			elif( freqList[i] == -1 ):
				addWord(word);
			else:
				freqList[i] += 1;
		except:
			addWord(word)

def updateAverages():
	global freqList ;
	global ageList;
	global prevFreqList
	global averageAgeList;
	global averageFreqList;

	
	i = 0;
	while True:
		try:
			#print "freqList is   " + str(freqList[i]);
			if( freqList[i] > 0 ):
				averageFreqList[i] = (averageFreqList[i]*(ageList[i]-1) + freqList[i])/ageList[i];
				averageAgeList[i] = ((averageAgeList[i]+1)*(prevFreqList[i]) + (freqList[i]-prevFreqList[i]))/freqList[i];
			i += 1
		except:
			#print "updating averages  " + str(i)
			return
				
				
	



def findValueOfRevns():
	conn = db.connectDb();
	#revisions = db.extractDataTbRev(conn);
	revisions = getRev1();
	
	uniqueRevs = getRev()
	pageValues = []
	
	filePageValues = open(cg.pageValues, 'w');

	i = 0;
	
	for rev in revisions:
		#print rev
		Text = db.extractTextForRev(conn, rev)[0].split(' ');
		initWordList()
		for word in Text:
			updateWordList(word.lower());
		try:
			uniqueRevs.index(rev) 
			updateAverages()
			#pageValue, pageValuef, pageValuefRoot, pageValueARoot = calculateValueOfRev()
			
			pageValue, pageValuef, pageValuefRoot, pageValueARoot, pageValueAvgf, pageValueAvgA = calculateValueOfRev()
			#pageValues.append([rev, pageValue, pageValuef]);
			#print i, rev, pageValue, pageValuef, pageValuefRoot, pageValueARoot;
			print i
			#print i, rev, pageValue, pageValuef, pageValuefRoot, pageValueARoot, pageValueAvgf, pageValueAvgA;
			filePageValues.write(rev + ' ' + str(pageValue) + ' ' + str(pageValuef) + ' ' + str(pageValuefRoot) + ' ' + str(pageValueARoot)+ ' ' + str(pageValueAvgf)+ ' ' + str(pageValueAvgA) + '\n');
			i += 1
		except:
			continue;
		
		cleanWordList();
	#print pageValues;	
	db.closeConn(conn)

findValueOfRevns()

